﻿using System;
using System.Collections.Generic;
using DAQ.Common;

namespace DAQ.PROTOCOL
{
    public class PC_LINK
    {
        public int COM_ID { get; set; }
        public int TENSION_DISPLAY_AVERAGE { get { return _bytes.SubByte(0, 2).BytesToInteger()/4; } }
        public int TENSION_SPEED_AVERAGE { get { return _bytes.SubByte(2, 2).BytesToInteger()/4; } }
        public int TENSION_REGULATION_AVERAGE { get { return _bytes.SubByte(4, 2).BytesToInteger()/4; } }
        public int TENSION_PEAK_MAX { get { return _bytes.SubByte(6, 2).BytesToInteger()/4; } }
        public int TENSION_PEAK_MIN { get { return _bytes.SubByte(8, 2).BytesToInteger()/4; } }
        public int OFFSET { get { return _bytes.SubByte(10, 2).BytesToInteger(); } }
        public int METER_TOTAL_COUNTER { get { return _bytes.SubByte(12, 4).BytesToInteger(); } }
        public double SPEED { get { return 60 * R * Math.Pow(10, -6) * Math.PI * (1.085 * _bytes.SubByte(16, 3).BytesToInteger()); } }
        public int INTEGRAL_TORQUE { get { return _bytes.SubByte(20, 2).BytesToInteger(); } }
        public int INTEGRAL_REVERSE { get { return _bytes.SubByte(22, 2).BytesToInteger(); } }
        public int CURRENT_CONSUMPTION { get { return _bytes.SubByte(24, 1).BytesToInteger(); } }
        public int BOARD_TEMPERATURE { get { return _bytes.SubByte(25, 1).BytesToInteger(); } }
        public int TENSION_SET_POINT { get { return _bytes.SubByte(26, 2).BytesToInteger(); } }
        public int WORK_TENSION { get { return _bytes.SubByte(28, 2).BytesToInteger(); } }
        public int TENSION_ERROR_COUNTER { get { return _bytes.SubByte(30, 1).BytesToInteger(); } }
        public int E2_ALARM_COUNTER { get { return _bytes.SubByte(31, 1).BytesToInteger(); } }
        public int E3_ALARM_COUNTER { get { return _bytes.SubByte(32, 1).BytesToInteger(); } }
        public int BOARD_TEMPERATURE_ALARM_COUNTER { get { return _bytes.SubByte(33, 1).BytesToInteger(); } }
        public int VBUS_ALARM_COUNTER { get { return _bytes.SubByte(34, 1).BytesToInteger(); } }
        public int MOTOR_1_TEMEPERATURE_ALARM_COUNTER { get { return _bytes.SubByte(35, 1).BytesToInteger(); } }
        public int MOTOR_2_TEMPERATURE_ALARM_COUNTER { get { return _bytes.SubByte(36, 1).BytesToInteger(); } }
        public int MOTOR_1_CURENT_ALARM_COUNTER { get { return _bytes.SubByte(37, 1).BytesToInteger(); } }
        public int MOTOR_2_CURENT_ALARM_COUNTER { get { return _bytes.SubByte(38, 1).BytesToInteger(); } }
        public int STEPPER_ALARM_COUNTER { get { return _bytes.SubByte(39, 1).BytesToInteger(); } }
        public bool VBUS_ALARM { get { return ((ALARM_STATUS_A & (1 << 7)) > 0); } }
        public bool MOTOR_2_ALARM { get { return ((ALARM_STATUS_A & (1 << 6)) > 0); } }
        public bool MOTOR_1_ALARM { get { return ((ALARM_STATUS_A & (1 << 5)) > 0); } }
        public bool TARGET_ALARM { get { return ((ALARM_STATUS_A & (1 << 4)) > 0); } }
        public bool TEMPERATURE_ALARM { get { return ((ALARM_STATUS_A & (1 << 3)) > 0); } }
        public bool E3_ALARM { get { return ((ALARM_STATUS_A & (1 << 2)) > 0); } }
        public bool E2_ALARM { get { return ((ALARM_STATUS_A & (1 << 1)) > 0); } }
        public bool TENSION_ERROR { get { return ((ALARM_STATUS_A & (1 << 0)) > 0); } }
        public bool WARNING { get { return ((ALARM_STATUS_B & (1 << 7)) > 0); } }
        public bool STEPPER_HIGH_ERROR { get { return ((ALARM_STATUS_B & (1 << 6)) > 0); } }
        public bool STEPPER_LOW_ERROR { get { return ((ALARM_STATUS_B & (1 << 6)) > 0); } }
        public bool INPUT_CURRENT_ALARM { get { return ((ALARM_STATUS_B & (1 << 1)) > 0); } }
        public bool TENSION_ERROR_MEMORY { get { return ((ALARM_STATUS_B & (1 << 0)) > 0); } }
        public byte ALARM_STATUS_A { get { return _bytes.SubByte(40, 1)[0]; } }
        public byte ALARM_STATUS_B { get { return _bytes.SubByte(41, 1)[0]; } }
        public byte STATUS_A { get { return _bytes.SubByte(42, 1)[0]; } }
        public byte STATUS_B { get { return _bytes.SubByte(43, 1)[0]; } }
        public byte TENSION_END_SCALE { get { return _bytes.SubByte(44, 1)[0]; } }
        public byte MOTOR_1_TEMPERATURE { get { return _bytes.SubByte(46, 1)[0]; } }
        public byte MOTOR_2_TEMPERATURE { get { return _bytes.SubByte(47, 1)[0]; } }
        public int R { get => r; set => r = value; }

        public Dictionary<string, string> AllValues { get {
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                foreach (var a in this.GetType().GetProperties())
                {
                    if (a.Name != "AllValues")
                    {
                        dictionary.Add(a.Name, a.GetValue(this).ToString());
                    }
                }
                return dictionary;
            } }

        int r = 1;

        byte[] _bytes = new byte[52];
        public PC_LINK(byte[] srcBytes)
        {
            _bytes = srcBytes;
        }
        public static byte[] GetSendBytes(byte Addr)
        {
            return BasicUtil.AppendChecksum(Addr, 0x3b);
        }
    }
}
    

    


